package com.jd.webapp.portal.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.jd.common.convention.TTResult;
import com.jd.common.util.HttpClientUtil;
import com.jd.common.util.JsonUtils;
import com.jd.goods.pojo.Goods;
import com.jd.goods.pojo.GoodsDescription;
import com.jd.goods.pojo.GoodsSpecification;
import com.jd.goods.service.GoodsDescService;
import com.jd.goods.service.GoodsService;
import com.jd.goods.service.GoodsSpecificationService;
import com.jd.webapp.portal.pojo.GoodsVo;

@Service
public class GoodsServiceDtoImpl implements GoodsServiceDto {
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsDescService goodsDescService;
	@Autowired
	private GoodsSpecificationService goodsSpecService;
	
	@Override
	public GoodsVo getGoodsById(long id) throws Exception {
		Goods goods = goodsService.getGoodsById(id);
		GoodsVo gvo = new GoodsVo();
		gvo.setBarcode(goods.getBarcode());
		gvo.setId(goods.getId());
		gvo.setImage(goods.getImage());
		gvo.setNum(goods.getNum());
		gvo.setCid(goods.getCid());
		gvo.setPrice(goods.getPrice());
		gvo.setSellPoint(goods.getSellPoint());
		gvo.setStatus(goods.getStatus());
		gvo.setTitle(goods.getTitle());
		return gvo;	
	}

	@Override
	public GoodsDescription getGoodsDescByGoodsId(long goodsid) throws Exception {
		return goodsDescService.getGoodsDescByGoodsId(goodsid);
	}

	/*
	 *[{"group":"主体","params":[{"k":"型号","v":"拯救者"},{"k":"颜色","v":"黑色"}]},{"group":"操作系统","params":[{"k":"操作系统","v":"Windows 10"}]},{"group":"内存","params":[{"k":"内存容量","v":"8GB"}]}]
	 */
	@Override
	public String getGoodsSpecByGoodsId(long goodsid) throws Exception {
		//要返回的html片段
		StringBuilder sb = new StringBuilder();
		//添加头
		sb.append("<div class=\"Ptable\">");
		GoodsSpecification goodsSpec = goodsSpecService.getGoodsSpecificationByGoodsId(goodsid);
		String data = goodsSpec.getSpecifications();
		List<Map> list = JsonUtils.jsonToList(data, Map.class);
		for(Map map : list){
			String group = (String)map.get("group");
			//添加组
			sb.append("<div class=\"Ptable-item\">");
			sb.append("<h3>"+map.get("group")+"</h3>");
			List<Map> params = (List<Map>) map.get("params");
			//添加参数头部
			sb.append("<dl>");
			for(Map param : params){
				//添加key
				sb.append("<dt>"+param.get("k")+"</dt>");
				//添加value
				sb.append("<dd>"+param.get("v")+"</dd>");
			}
			//添加参数尾部
			sb.append("</dl>");
			//添加组尾部
			sb.append("</div>");
		}
		//添加尾部
		sb.append("</div>");
		return sb.toString();
	}

}
